<%#-- copyright
OpenProject is an open source project management software.
Copyright (C) 2012-2020 the OpenProject GmbH

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License version 3.

OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
Copyright (C) 2006-2017 Jean-Philippe Lang
Copyright (C) 2010-2013 the ChiliProject Team

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

See docs/COPYRIGHT.rdoc for more details.

++#%>

<% cost_types = @cost_types.reject(&:deleted_at) -%>

<% if cost_types.empty? %>
  <%= no_results_box %>
<% else %>
  <div class="generic-table--container">
    <div class="generic-table--results-container">
      <table class="generic-table">
        <colgroup>
          <col highlight-col>
          <col highlight-col>
          <col highlight-col>
          <col highlight-col>
          <col highlight-col>
          <col highlight-col>
          <col>
        </colgroup>
        <thead>
          <tr>
            <%= sort_header_tag 'name', :caption => CostType.model_name.human %>
            <%= sort_header_tag 'unit', :caption => CostType.human_attribute_name(:unit) %>
            <%= sort_header_tag 'unit_plural', :caption => CostType.human_attribute_name(:unit_plural) %>
            <th>
              <div class="generic-table--sort-header-outer">
                <div class="generic-table--sort-header">
                  <span>
                    <%= CostType.human_attribute_name(:current_rate) %>
                  </span>
                </div>
              </div>
            </th>
            <th>
              <div class="generic-table--sort-header-outer">
                <div class="generic-table--sort-header">
                  <span>
                    <%= t(:caption_set_rate) %>
                  </span>
                </div>
              </div>
            </th>
            <th>
              <div class="generic-table--sort-header-outer">
                <div class="generic-table--sort-header">
                  <span>
                    <%= t(:caption_default) %>
                  </span>
                </div>
              </div>
            </th>
            <th><div class="generic-table--empty-header"></div></th>
          </tr>
        </thead>
        <tbody>
          <% cost_types.each do |cost_type| %>
          <tr>
            <%= content_tag :td, link_to(cost_type.name, {:controller => '/cost_types', :action => 'edit', :id => cost_type}) %>
            <%= content_tag :td, cost_type.unit %>
            <%= content_tag :td, cost_type.unit_plural %>
            <%= content_tag :td, to_currency_with_empty(cost_type.rate_at(@fixed_date)), :class => "currency", :id => "cost_type_#{cost_type.id}_rate"%>
            <td>
              <%= form_for cost_type, :url => { :controller => '/cost_types', :action => 'set_rate', :id => cost_type }, method: :put, html: { class: 'inline-label' }  do |f| %>
                <label class="hidden-for-sighted" for="<%= "rate_field_#{cost_type.id}" %>"><%= t(:caption_set_rate) %></label>
                <%= content_tag :input, '', :value => "", :name => :rate, :size => 7, :id => "rate_field_#{cost_type.id}" %>
                <span class="form-label">
                  <%= Setting.plugin_openproject_costs['costs_currency'] %>
                </span>
                <button type="submit"
                        class="button submit_cost_type">
                  <%= icon_wrapper('icon icon-save', I18n.t(:caption_save_rate)) %>
                </button>
              <% end %>
            </td>
            <%= content_tag :td, cost_type.is_default? ? icon_wrapper('icon icon-checkmark', I18n.t(:general_text_Yes)) : "" %>
            <td class="buttons">
              <%= form_for cost_type, url:    cost_type_path(cost_type),
                                      method: :delete,
                                      html:   { id:    "delete_cost_type_#{cost_type.id}",
                                                class: 'delete_cost_type',
                                                title: t(:button_lock) } do |f| %>
                <button type="submit"
                        class="button--link submit_cost_type">
                  <%= icon_wrapper('icon icon-locked', I18n.t(:button_lock)) %>
                </button>
              <% end %>
            </td>
          </tr>
          <tr style="display:none;" >
          </tr>
          <% end %>
        </tbody>
      </table>

    </div>
  </div>
<%# Moved from assets since this is the only remaining confirmation JS from the old costs code %>
<%= nonced_javascript_tag  do %>
  function submitForm(event, el) {
    submitFormWithConfirmation(event, el, true);
  }

  function submitFormWithConfirmation(event, el, withConfirmation) {
    event.preventDefault();

    if (!withConfirmation || confirm(I18n.t("js.text_are_you_sure"))) {
      jQuery(el).parent().submit();
    }

    return false;
  }
<% end %>
<% end %>
